from mesh_entry import *

class mdict(dict):
	"""A subclass of the standard dictionary which doesn't overwrite the original key value but
	appends it to a list
	"""
	def __setitem__(self, key, value):
		"""add the given value to the list of values for this key"""
		self.setdefault(key, []).append(value)

class mesh_node():
	"""Contains id, children, description number"""
	def __init__(self, id, name, children=None, parent=None):
		self.id = id
		self.name = name
		if children == None:
			self.children = []
		else:
			self.children = children
		self.parent = parent
		self.des_pos = -1
		self.entry = None
		
	def add_child(self, child):
		"""Adds a single mesh_node as child"""
		self.children.append(child)
		
	def set_parent(self, parent):
		"""Sets the parent"""
		self.parent = parent
		
	def set_children(self, children):
		"""sets a list as children"""
		self.children = children
		
	def get_ancestors(self):
		"""Gives all the ancestors of this node till the root"""
		iter = self.parent
		ancestors = []
		while(iter):
			ancestors.append(iter)
			iter = iter.parent
		return ancestors
	
	def __str__(self):
		if self.id == None:
			self.id = ''
		return 'Mesh node, id = ' + self.id + ' name = ' + self.name